package user.view;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.border.Border;

import system.activity.MainListener;
import system.util.HistoryReader;
import system.util.ReportUtil;
import system.util.SettingsUtil;

public class ReportPanelImpl extends JPanel
{
	private javax.swing.JButton createReportButton;
	private javax.swing.JButton closeButton;
	private javax.swing.JLabel titleReportLabel;
	private javax.swing.JLabel descriptionReportLabel;
	private javax.swing.JTextField titleReportText;
	private javax.swing.JTextArea descriptionReportText;
	MainListener listener;

	public ReportPanelImpl(MainListener listener)
	{
		this.listener = listener;
		init();
		this.listener.addRightPanel(this);
		this.show();
	}

	private void init()
	{
		titleReportLabel = new javax.swing.JLabel();
		titleReportText = new javax.swing.JTextField();
		createReportButton = new javax.swing.JButton();
		descriptionReportLabel = new javax.swing.JLabel();
		descriptionReportText = new javax.swing.JTextArea();
		closeButton = new javax.swing.JButton();

		titleReportLabel.setText("Заголовок");
		titleReportLabel.setName("titleReportLabel");

		titleReportText.setText("");
		titleReportText.setName("titleReportText");

		createReportButton.setText("Создать отчет");
		createReportButton.setName("createReportButton");

		closeButton.setText("Отмена");
		closeButton.setName("closeButton");

		descriptionReportLabel.setText("Добавления и заметки");
		descriptionReportLabel.setName("descriptionReportLabel");

		descriptionReportText.setText("");
		descriptionReportText.setName("descriptionReportText");
		descriptionReportText.setLineWrap(true);
		descriptionReportText.setDragEnabled(false);
		javax.swing.GroupLayout ReportPanelLayout = new javax.swing.GroupLayout(this);
		this.setLayout(ReportPanelLayout);
		ReportPanelLayout.setHorizontalGroup(ReportPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				ReportPanelLayout
						.createSequentialGroup()
						.addContainerGap()
						.addGroup(
								ReportPanelLayout
										.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
										.addComponent(createReportButton, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
										.addComponent(closeButton, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
										.addGroup(
												javax.swing.GroupLayout.Alignment.LEADING,
												ReportPanelLayout.createSequentialGroup().addComponent(titleReportLabel).addGap(15, 15, 15)
														.addComponent(titleReportText, javax.swing.GroupLayout.DEFAULT_SIZE, 145, Short.MAX_VALUE))
										.addComponent(descriptionReportLabel, javax.swing.GroupLayout.Alignment.LEADING)
										.addComponent(descriptionReportText, javax.swing.GroupLayout.PREFERRED_SIZE, 217,
												javax.swing.GroupLayout.PREFERRED_SIZE)).addContainerGap()));
		ReportPanelLayout.setVerticalGroup(ReportPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				ReportPanelLayout
						.createSequentialGroup()
						.addContainerGap()
						.addGroup(
								ReportPanelLayout
										.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
										.addComponent(titleReportText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
												javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(titleReportLabel))
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(descriptionReportLabel)
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
						.addComponent(descriptionReportText, javax.swing.GroupLayout.DEFAULT_SIZE, 135, Short.MAX_VALUE)
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(createReportButton).addComponent(closeButton)
						.addContainerGap()));
		closeButton.addMouseListener(new java.awt.event.MouseAdapter()
		{
			public void mouseClicked(java.awt.event.MouseEvent evt)
			{
				listener.removeRihtPanel();
				validate();
				listener.needRepaint();
			}
		});
		createReportButton.addMouseListener(new java.awt.event.MouseAdapter()
		{
			public void mouseClicked(java.awt.event.MouseEvent evt)
			{
				try {
					listener.needRepaint();
					ArrayList<String> resultRows = new ArrayList<String>();
					resultRows.add("");
					resultRows.add(titleReportText.getText());
					resultRows.addAll(listener.getControlDescription());
					resultRows.add(descriptionReportLabel.getText());
					resultRows.add(descriptionReportText.getText());

					JFileChooser fileChooser = new JFileChooser();
					if (new File(SettingsUtil.getDefaultFolder()) != null)
						fileChooser.setCurrentDirectory(new File(SettingsUtil.getDefaultFolder()));
					int ret = fileChooser.showDialog(null, "Создать отчет");
					if (ret == JFileChooser.APPROVE_OPTION) {
						File file = fileChooser.getSelectedFile();
						HistoryReader.saveDOE(listener.getControlHistoryDescription(), file.getAbsolutePath());
						ReportUtil.createReport(resultRows, (ArrayList<BufferedImage>) listener.getImages(), file.getAbsolutePath());

						SettingsUtil.setDefaultFolder(file.getParent());

					}
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		Border border = BorderFactory.createEtchedBorder(Color.white, new Color(178, 178, 178));
		this.setBorder(border);
	}

}
